<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="name">
              <el-input v-model="filters.name" clearable filterable placeholder="云喇叭名称"></el-input>
            </el-form-item>
            <el-form-item prop="sn">
              <el-input v-model="filters.sn" clearable filterable placeholder="云喇叭编号"></el-input>
            </el-form-item>
            <el-form-item prop="type">
              <el-select v-model="filters.type" clearable filterable placeholder="云喇叭类型">
                <el-option v-for="item in hornTypeList" :key="item.id" :value="item.id" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="hornState">
              <el-select v-model="filters.hornState" clearable filterable placeholder="云喇叭状态">
                <el-option v-for="item in hornStateList" :key="item.id" :value="item.id" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item prop="businessName">
              <el-input v-model="filters.businessName" clearable filterable placeholder="商户名称"></el-input>
            </el-form-item> -->
            <!--<el-form-item prop="businessId">
              <el-select v-model="filters.businessId" placeholder="请选择商户" clearable filterable>
                <el-option v-for="item in businessList" :label="item.name" :value="item.id" :key="item.id">

                </el-option>
              </el-select>
            </el-form-item>-->
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增 </el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table ref="tableList" :data="dataList" :fit="true" highlight-current-row element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column prop="name" label="云喇叭名称" align="center"></el-table-column>
        <el-table-column label="云喇叭类型" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="scope.row.type == 1 ? '' : 'success'">
                {{ scope.row.type == 1 ? '商户' : '平台' }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="sn" label="云喇叭编号" align="center"></el-table-column>
        <!-- <el-table-column prop="printerKey" label="设备密钥" align="center"></el-table-column> -->
        <!-- <el-table-column prop="businessName" label="商户" align="center"></el-table-column> -->
        <el-table-column label="云喇叭状态" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatHornStates(scope.row.hornState)">
                {{ formatHornState(scope.row.hornState) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatStates(scope.row.state)">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑 </el-dropdown-item>
                <el-dropdown-item v-has="permissionActivation" @click.native="hanldeActive(scope.row)" v-if="scope.row.hornState == 0">激活设备 </el-dropdown-item>
                <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)">{{ scope.row.state === 0 ? '启用' : '禁用' }} </el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)" v-if="scope.row.state === 0">删除 </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import HornApi from '@/api/equip/cloudTrumpet';
import BusinessApi from '@/api/business/business';

export default {
  name: 'cloudTrumpetManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      hornTypeList: [
        { id: 1, name: '商户' },
        { id: 2, name: '平台' },
      ],
      hornStateList: [
        {
          id: '0',
          name: '未激活',
        },
        {
          id: '1',
          name: '已激活',
        },
        {
          id: '2',
          name: '在线',
        },
        {
          id: '3',
          name: '离线',
        },
        {
          id: '4',
          name: '已注销',
        },
      ],
      filters: {
        name: '',
        sn: '',
        type: '',
        hornState: '',
        // businessName: "",
        // businessId: "",
      },
      businessList: [],
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      // 权限

      permissionAdd: 'SYSTEM_MANAGER_EQUIP_HORN_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_EQUIP_HORN_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_EQUIP_HORN_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_EQUIP_HORN_LIST_DELETE',
      permissionAlarm: 'SYSTEM_MANAGER_EQUIP_HORN_LIST_ALARM',
      permissionUpStatus: 'SYSTEM_MANAGER_EQUIP_HORN_LIST_STATE',
      permissionActivation: 'SYSTEM_MANAGER_EQUIP_HORN_LIST_ACTIVATION',
    };
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      this.listData();
      this.getTableHeight();
      // this.getBusinessList()
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    getBusinessList() {
      BusinessApi.listIdNameVo().then((res) => {
        this.businessList = res.data;
      });
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    formatState(cellValue) {
      if (cellValue === -1) {
        return '删除';
      } else if (cellValue === 0) {
        return '禁用';
      } else if (cellValue === 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue === -1) {
        return 'danger';
      } else if (cellValue === 0) {
        return 'info';
      } else if (cellValue === 1) {
        return 'success';
      }
    },
    formatHornState(cellValue) {
      if (cellValue === 0) {
        return '未激活';
      } else if (cellValue === 1) {
        return '已激活';
      } else if (cellValue === 2) {
        return '在线';
      } else if (cellValue === 3) {
        return '离线';
      } else if (cellValue === 4) {
        return '已注销';
      }
    },
    formatHornStates(cellValue) {
      if (cellValue === 0) {
        return '';
      } else if (cellValue === 1 || cellValue === 2) {
        return 'success';
      } else if (cellValue === 3 || cellValue === 4) {
        return 'info';
      }
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        name: _this.filters.name,
        sn: _this.filters.sn,
        type: _this.filters.type,
        hornState: _this.filters.hornState,
      };
      // 发送请求
      HornApi.listHorn(cond)
        .then((res) => {
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
          setTimeout(() => {
            this.$refs.tableList.doLayout();
          });
        })
        .catch((err) => {});
    },
    // 激活设备
    hanldeActive(row) {
      this.$confirm('确定激活设备吗？', '提示', {}).then((res) => {
        HornApi.activeEquip({ id: row.hornId }).then((res) => {
          this.$Utils.notifySuccess('激活成功');
          // this.getData()
          this.listData();
        });
      });
    },
    handleIsDisable(row) {
      let _this = this;
      let currentState = row.state === 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        HornApi.deleteHorn({
          id: row.hornId,
          state: currentState,
        }).then((res) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDelete(val) {
      // 删除
      this.$confirm('是否删除该云喇叭', message.TITLE_PROMPT, {}).then(() => {
        var cond = {
          id: val.hornId,
          state: -1,
        };
        HornApi.deleteHorn(cond).then((data) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/equip/cloudTrumpetDetail',
        query: { id: row.hornId },
      });
    },
    handleClickEditor(val) {
      //编辑 进入新增页通过id来查找数据
      let _this = this;
      _this.$router.push({
        path: '/equip/cloudTrumpetEdit',
        query: { id: val.hornId },
      });
    },
    handleClickAdd() {
      // 刷新
      let _this = this;
      _this.$router.push('/equip/cloudTrumpetAdd');
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
